45 #define ENVP_PAGE 0x100
46 #define ARGV_PAGE 0x100
48 #define STACK_PAD 0x1000
63 #define AUXARGS_ENTRY(pos, id, val) {*pos = id;pos++; *pos = val;pos++;}
65 static int argv_count(
char **argv) {
68 while (*argv++ != 0x0)
74 static int envp_count(
char **envp) {
77 while (*envp++ != 0x0)
83 static int args_copyin(
char **argv_in,
char **argv_out,
char **args_out) {
85 int argc = argv_count(argv_in);
97 for (i = 1; i <= argc; i++) {
98 argv_tmp[i] = (
uint32_t)(args_tmp + sp);
99 strcpy((
char *)argv_tmp[i], argv_in[i - 1]);
100 sp +=
strlen(argv_in[i - 1]) + 1;
105 *argv_out = (
char *)argv_tmp;
106 *args_out = args_tmp;
112 static int envs_copyin(
char **envp_in,
char **envp_out,
char **envs_out) {
114 int envc = envp_count(envp_in);
124 for (i = 0; i < envc; i++) {
125 envp_tmp[i] = (
uint32_t)(envs_tmp + sp);
126 strcpy((
char *)envp_tmp[i], envp_in[i]);
127 sp +=
strlen(envp_in[i]) + 1;
131 *envp_out = (
char *)envp_tmp;
132 *envs_out = envs_tmp;
163 newProcess->
tss.
ss0 = 0x0;
165 newProcess->
tss.
ss1 = 0x0;
167 newProcess->
tss.
ss2 = 0x0;
169 newProcess->
tss.
eip = (
unsigned int) tproc;
171 newProcess->
tss.
esp = stackAddr + (stack - 0x4);
172 newProcess->
tss.
ebp = 0x0;
173 newProcess->
tss.
esi = 0x0;
174 newProcess->
tss.
edi = 0x0;
187 newProcess->
tss.
es = 0x10;
188 newProcess->
tss.
cs = 0x08;
189 newProcess->
tss.
ss = 0x10;
190 newProcess->
tss.
ds = 0x10;
191 newProcess->
tss.
fs = 0x10;
192 newProcess->
tss.
gs = 0x10;
194 newProcess->
tss.
ldt = 0x18;
199 if (newProcess->
files[0] != 0x0)
200 kpanic(
"Problem With File Descriptors");
202 newProcess->
files[0] = 0x0;
209 "movl %%esp,%%ecx \n"
211 "movl %%eax,%%esp \n"
215 "movl %%esp,%%eax \n"
217 "movl %%ecx,%%esp \n"
220 :
"b" (arg),
"m" (newProcess->
tss.
esp)
246 void execFile(
char *
file,
char **argv,
char **envp,
int console) {
255 Elf_Ehdr *binaryHeader = 0x0;
257 Elf_Phdr *programHeader = 0x0;
259 int argc = argv_count(argv);
260 int envc = envp_count(envp);
266 newProcess->
gid = 0x0;
267 newProcess->
uid = 0x0;
268 newProcess->
pgrp = newProcess->
id;
271 if (newProcess->
term == 0x0)
272 kprintf(
"Error: invalid console\n");
283 "movl %%eax,%%cr3 \n"
288 if (newProcess->
files[0] != 0x0)
289 kpanic(
"Problem With File Descriptors");
293 if (newProcess->
files[0] == 0x0) {
294 kprintf(
"Exec Format Error: Binary File Not Executable1.\n");
300 kprintf(
"Exec Format Error: Binary File Not Executable2.\n");
306 binaryHeader = (Elf_Ehdr *)
kmalloc(
sizeof(Elf_Ehdr));
308 fread(binaryHeader,
sizeof(Elf_Ehdr), 1, newProcess->
files[0]);
311 if ((binaryHeader->e_ident[1] !=
'E') && (binaryHeader->e_ident[2] !=
'L') && (binaryHeader->e_ident[3] !=
'F')) {
312 kprintf(
"Exec Format Error: Binary File Not Executable3.\n");
317 else if (binaryHeader->e_type != 2) {
318 kprintf(
"Exec Format Error: Binary File Not Executable4.\n");
323 else if (binaryHeader->e_entry == 0x300000) {
324 kprintf(
"Exec Format Error: Binary File Not Executable5.\n");
333 programHeader = (Elf_Phdr *)
kmalloc(
sizeof(Elf_Phdr) * binaryHeader->e_phnum);
334 fseek(newProcess->
files[0], binaryHeader->e_phoff, 0);
336 fread(programHeader, (
sizeof(Elf_Phdr) * binaryHeader->e_phnum), 1, newProcess->
files[0]);
339 for (i = 0; i < binaryHeader->e_phnum; i++) {
340 if (programHeader[i].p_type == 1) {
345 for (x = 0x0; x < (programHeader[i].p_memsz); x += 0x1000) {
350 memset((
void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000);
355 fseek(newProcess->
files[0], programHeader[i].p_offset, 0);
357 fread((
void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, newProcess->
files[0]);
359 if ((programHeader[i].p_flags & 0x2) != 0x2) {
360 for (x = 0x0; x < (programHeader[i].p_memsz); x += 0x1000) {
362 kpanic(
"Error: vmm_setPageAttributes failed, File: %s, Line: %i\n", __FILE__, __LINE__);
375 for (x = 1; x <= 100; x++) {
391 newProcess->
tss.
esp0 = 0xFFFFFFFF;
392 newProcess->
tss.
ss0 = 0x10;
394 newProcess->
tss.
ss1 = 0x0;
396 newProcess->
tss.
ss2 = 0x0;
397 newProcess->
tss.
eip = (long) binaryHeader->e_entry;
400 newProcess->
tss.
ebp = 0x0;
401 newProcess->
tss.
esi = 0x0;
402 newProcess->
tss.
edi = 0x0;
405 newProcess->
tss.
es = 0x30 + 3;
406 newProcess->
tss.
cs = 0x28 + 3;
407 newProcess->
tss.
ss = 0x30 + 3;
408 newProcess->
tss.
ds = 0x30 + 3;
409 newProcess->
tss.
fs = 0x30 + 3;
410 newProcess->
tss.
gs = 0x8 + 3 + 4;
412 newProcess->
tss.
ldt = 0x18;
419 kfree(programHeader);
421 newProcess->
files[0] = 0x0;
425 tmp[0] = binaryHeader->e_entry;
436 for (i = 1; i <= argc; i++) {
438 strcpy((
char *) tmp[i], argv[i - 1]);
439 sp +=
strlen(argv[i - 1]) + 1;
445 for (
int x = 0; x < envc; x++) {
447 strcpy((
char *) tmp[x + i], envp[x]);
448 sp +=
strlen(envp[x]) + 1;
456 K_PANIC(
"Error: Remap Page Failed");
464 taskLDT->
limitLow = (0xFFFFF & 0xFFFF);
465 taskLDT->
baseLow = (data_addr & 0xFFFF);
466 taskLDT->
baseMed = ((data_addr >> 16) & 0xFF);
470 taskLDT->
baseHigh = data_addr >> 24;
476 "movl %%eax,%%cr3 \n"
498 int argc = argv_count(argv);
499 int envc = envp_count(envp);
513 Elf_Ehdr *binaryHeader = 0x0;
514 Elf_Phdr *programHeader = 0x0;
515 Elf_Shdr *sectionHeader = 0x0;
519 u_long text_addr = 0, text_size = 0;
520 u_long data_addr = 0, data_size = 0;
524 asm(
"movl %%cr3, %0;" :
"=r" (cr3));
534 if (fd->
perms == 0) {
535 kprintf(
"Exec Format Error: Binary File Not Executable6.\n");
545 char *args_out = 0x0;
547 args_copyin(argv, (
char **)&argv_out, &args_out);
550 char *envs_out = 0x0;
552 envs_copyin(envp, (
char **)&envp_out, &envs_out);
562 for (x = 1; x <= 100; x++) {
568 if ((binaryHeader = (Elf_Ehdr *)
kmalloc(
sizeof(Elf_Ehdr))) == 0x0)
571 fread(binaryHeader,
sizeof(Elf_Ehdr), 1, fd);
575 if ((binaryHeader->e_ident[1] !=
'E') && (binaryHeader->e_ident[2] !=
'L') && (binaryHeader->e_ident[3] !=
'F')) {
576 kprintf(
"Exec Format Error: Binary File Not Executable7.\n");
581 else if (binaryHeader->e_type !=
ET_EXEC) {
582 kprintf(
"Exec Format Error: Binary File Not Executable8.\n");
587 else if (binaryHeader->e_entry == 0x300000) {
588 kprintf(
"Exec Format Error: Binary File Not Executable9.\n");
598 if ((programHeader = (Elf_Phdr *)
kmalloc(
sizeof(Elf_Phdr) * binaryHeader->e_phnum)) == 0x0)
603 fseek(fd, binaryHeader->e_phoff, 0);
604 fread(programHeader, (
sizeof(Elf_Phdr) * binaryHeader->e_phnum), 1, fd);
608 if ((sectionHeader = (Elf_Shdr *)
kmalloc(
sizeof(Elf_Shdr) * binaryHeader->e_shnum)) == 0x0)
612 fseek(fd, binaryHeader->e_shoff, 0);
613 fread(sectionHeader,
sizeof(Elf_Shdr) * binaryHeader->e_shnum, 1, fd);
620 for (i = 0; i < binaryHeader->e_phnum; i++) {
621 switch (programHeader[i].p_type) {
623 if (programHeader[i].p_memsz == 0x0)
626 seg_addr =
trunc_page(programHeader[i].p_vaddr);
627 seg_size =
round_page(programHeader[i].p_memsz + programHeader[i].p_vaddr - seg_addr);
633 for (x = 0x0; x < (
round_page(programHeader[i].p_memsz)); x += 0x1000) {
636 K_PANIC(
"Error: Remap Page Failed");
642 memset((
void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000);
647 fseek(fd, programHeader[i].p_offset, 0);
648 fread((
void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, fd);
650 if ((programHeader[i].p_flags & 0x2) != 0x2) {
651 for (x = 0x0; x < (
round_page(programHeader[i].p_memsz)); x += 0x1000) {
653 kpanic(
"Error: vmm_setPageAttributes failed, File: %s,Line: %i\n", __FILE__, __LINE__);
657 if ((programHeader[i].p_flags &
PF_X) && text_size < seg_size) {
659 text_size = seg_size;
660 text_addr = seg_addr;
664 data_size = seg_size;
665 data_addr = seg_addr;
682 ef->
dynamic = (Elf_Dyn *) programHeader[i].p_vaddr;
688 kprintf(
"%s:%i>Malloc: %i\n", _FILE_,_LINE_,programHeader[i].p_filesz);
690 interp = (
char *)
kmalloc(programHeader[i].p_filesz);
691 fseek(fd, programHeader[i].p_offset, 0);
692 fread((
void *) interp, programHeader[i].p_filesz, 1, fd);
694 kprintf(
"Interp: [%s]\n", interp);
716 elf_parse_dynamic(ef);
725 iFrame->
eip = ldAddr;
728 iFrame->
eip = binaryHeader->e_entry;
745 for (i = 1; i <= argc; i++) {
748 EXECP = (
char *)tmp[i];
750 strcpy((
char *)tmp[i], (
const char *)argv_out[i]);
752 kprintf(
"argv[%i]:%s",i, (
const char *)argv_out[i]);
754 sp +=
strlen((
const char *)argv_out[i]) + 1;
766 for (x = 0; x < envc; x++) {
768 strcpy((
char *) tmp[x + i], (
const char *)envp_out[x]);
769 sp +=
strlen((
const char *)envp_out[x]) + 1;
779 struct file *tFP = 0x0;
794 tmp[i++] = binaryHeader->e_phoff + 0x08048000;
798 tmp[i++] = binaryHeader->e_phentsize;
802 tmp[i++] = binaryHeader->e_phnum;
816 tmp[i++] = binaryHeader->e_entry;
844 kfree(programHeader);
879 K_PANIC(
"Error: Remap Page Failed");
888 taskLDT->
limitLow = (0xFFFFF & 0xFFFF);
889 taskLDT->
baseLow = (data_addr & 0xFFFF);
890 taskLDT->
baseMed = ((data_addr >> 16) & 0xFF);
894 taskLDT->
baseHigh = data_addr >> 24;
907 switch (dynp->
d_tag) {
920 const Elf_Hashelt *hashtab = (
const Elf_Hashelt *) (ef->
address + dynp->
d_un.
d_ptr);
962 if (dynp->
d_un.
d_val !=
sizeof(Elf_Rela))