int get_arch_from_elf(const char *file) { int fd, nbytes, class; struct stat st; struct elf_hdr_s elf_hdr; if (stat(file, &st)) return -1; if (!S_ISREG(st.st_mode)) return -1; fd = open(file, O_RDONLY); if (fd < 0) return -1; nbytes = read(fd, (void *) &elf_hdr, sizeof(elf_hdr)); close(fd); if (nbytes < (int)sizeof(elf_hdr)) return -1; if (check_elf_magic(elf_hdr.ident)) return -1; class = elf_hdr.ident[4]; switch (class) { case ELFCLASS32: return elf_32; case ELFCLASS64: return elf_64; } return elf_none; }
int load_elf(u64 pid, void *elf_start, unsigned *entry, unsigned *stack) { Elf32_Ehdr *elf; Elf32_Phdr *ph; elf = (Elf32_Ehdr *)elf_start; if (check_elf_magic(elf->e_ident) < 0) return -EINVAL; *entry = elf->e_entry; ph = ((Elf32_Phdr *)(elf_start + elf->e_phoff)); int j; void *void_ret; for (j = 0; j < elf->e_phnum; j++) { if (ph->p_type != PT_LOAD) { ph = (Elf32_Phdr *)(((char *)ph) + elf->e_phentsize); continue ; } int k = (ph->p_vaddr / 0x1000) * 0x1000; if ((int)(void_ret = rpc_mmap_sys(pid, (void *)k, 0, ph->p_memsz)) < 0) { print_error((int)void_ret); return (int)void_ret; } sys_invlpg((void *)((unsigned)void_ret / 0x1000)); memcpy(void_ret + (ph->p_vaddr % 0x1000), (void *)(elf_start + ph->p_offset), ph->p_filesz); if (ph->p_filesz != ph->p_memsz) { memset((void *)(void_ret + ((ph->p_vaddr + ph->p_filesz) %0x1000)), 0, ph->p_memsz - ph->p_filesz); *stack = (unsigned)(void_ret + ((ph->p_vaddr + ph->p_memsz) %0x1000)); } ph = (Elf32_Phdr *)(((char *)ph) + elf->e_phentsize); } return 0; }
int main() { Elf32_Ehdr *simple_elfh = APPLICATION_ELF(simple); Elf32_Ehdr *writer_elfh = APPLICATION_ELF(writer); Elf32_Ehdr *reader_elfh = APPLICATION_ELF(reader); Elf32_Ehdr *rtuapp_elfh = APPLICATION_ELF(rtuappv1); Elf32_Ehdr *sys_elfh = SYSTEM_ELF; if (check_elf_magic(sys_elfh)) INFO_MSG("System ELF magic checks out @ 0x%x\n", (u_int32_t)sys_elfh); else { ERROR_MSG("Wrong System ELF magic @ 0x%x\n", (u_int32_t)sys_elfh); goto exit; } /* * Registering tasks */ task_register_cons *simplec = task_register("simple", simple_elfh); task_register_cons *readerc = task_register("reader", reader_elfh); task_register_cons *writerc = task_register("writer", writer_elfh); task_register_cons *rtuappc = task_register("rtuapp", rtuapp_elfh); if (!task_alloc(simplec)) { ERROR_MSG("Could not alloc memory for task \"simple\"\n"); goto exit; } if (!task_alloc(readerc)) { ERROR_MSG("Could not alloc memory for task \"reader\"\n"); goto exit; } if (!task_alloc(writerc)) { ERROR_MSG("Could not alloc memory for task \"writer\"\n"); goto exit; } if (!task_alloc(rtuappc)) { ERROR_MSG("Could not alloc memory for task \"rtuapp\"\n"); goto exit; } /* * Linking tasks */ if (!task_link(simplec)) { ERROR_MSG("Could not link \"simple\" task\n"); goto exit; } if (!task_link(readerc)) { ERROR_MSG("Could not link \"reader\" task\n"); goto exit; } if (!task_link(writerc)) { ERROR_MSG("Could not link \"writer\" task\n"); goto exit; } if (!task_link(rtuappc)) { ERROR_MSG("Could not link \"rtuapp\" task\n"); goto exit; } /* * Starting tasks */ if (!task_start(simplec)) { ERROR_MSG("Could not start \"simple\" task\n"); goto exit; } if (!task_start(readerc)) { ERROR_MSG("Could not start \"reader\" task\n"); goto exit; } if (!task_start(writerc)) { ERROR_MSG("Could not start \"writer\" task\n"); goto exit; } if (!task_start(rtuappc)) { ERROR_MSG("Could not start \"rtuapp\" task\n"); goto exit; } /* * Create migration task. */ if (!migrator_start()) { ERROR_MSG("Could not start migrator.\n"); goto exit; } INFO_MSG("Starting scheduler\n"); vTaskStartScheduler(); exit: INFO_MSG("Going into infinite loop...\n"); while(1) ; }