2011-12-11 03:34:10 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
2011-04-15 05:32:19 +04:00
|
|
|
* ELF Static Executable Loader
|
|
|
|
*
|
|
|
|
* Part of the ToAru OS Kernel
|
|
|
|
* (C) 2011 Kevin Lange
|
|
|
|
* Released under the terms of the NCSA License, see the included
|
|
|
|
* README file for further information.
|
2011-04-09 00:27:12 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <system.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <elf.h>
|
2011-12-09 01:25:48 +04:00
|
|
|
#include <process.h>
|
2011-04-09 00:27:12 +04:00
|
|
|
|
|
|
|
/**
|
2011-04-15 05:32:19 +04:00
|
|
|
* Load and execute a static ELF binary.
|
|
|
|
*
|
|
|
|
* We make one assumption on the location the binary expects to be loaded
|
|
|
|
* at: that it be outside of the kernel memory space.
|
|
|
|
*
|
|
|
|
* Arguments are passed to the stack of the user application so that they
|
|
|
|
* can be read properly.
|
|
|
|
*
|
|
|
|
* TODO: Environment variables should be loaded somewhere.
|
|
|
|
*
|
|
|
|
* HACK: ELF verification isn't complete.
|
|
|
|
*
|
2011-04-09 00:27:12 +04:00
|
|
|
* @param path Path to the executable to attempt to execute.
|
|
|
|
* @param argc Number of arguments (because I'm not counting for you)
|
|
|
|
* @param argv Pointer to a string of arguments
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
exec(
|
2011-04-15 05:32:19 +04:00
|
|
|
char * path, /* Path to the executable to run */
|
|
|
|
int argc, /* Argument count (ie, /bin/echo hello world = 3) */
|
|
|
|
char ** argv /* Argument strings (including executable path) */
|
2011-04-09 00:27:12 +04:00
|
|
|
) {
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-04-16 02:33:19 +04:00
|
|
|
/* Open the file */
|
|
|
|
fs_node_t * file = kopen(path,0);
|
|
|
|
if (!file) {
|
|
|
|
/* Command not found */
|
2011-12-07 06:36:25 +04:00
|
|
|
return 0;
|
2011-04-16 02:33:19 +04:00
|
|
|
}
|
|
|
|
/* Read in the binary contents */
|
|
|
|
Elf32_Header * header = (Elf32_Header *)malloc(file->length + 100);
|
|
|
|
read_fs(file, 0, file->length, (uint8_t *)header);
|
2011-04-09 00:27:12 +04:00
|
|
|
|
2011-12-09 01:25:48 +04:00
|
|
|
current_process->name = malloc(strlen(path) + 1);
|
|
|
|
memcpy(current_process->name, path, strlen(path) + 1);
|
|
|
|
|
2011-04-16 02:33:19 +04:00
|
|
|
/* Alright, we've read the binary, time to load the loadable sections */
|
|
|
|
/* Verify the magic */
|
|
|
|
if ( header->e_ident[0] != ELFMAG0 ||
|
|
|
|
header->e_ident[1] != ELFMAG1 ||
|
|
|
|
header->e_ident[2] != ELFMAG2 ||
|
|
|
|
header->e_ident[3] != ELFMAG3) {
|
|
|
|
/* What? This isn't an ELF... */
|
|
|
|
kprintf("Fatal: Not a valid ELF executable.\n");
|
|
|
|
free(header);
|
|
|
|
close_fs(file);
|
2011-12-07 06:36:25 +04:00
|
|
|
return -1;
|
2011-04-16 02:33:19 +04:00
|
|
|
}
|
2011-04-09 00:27:12 +04:00
|
|
|
|
2011-12-09 01:25:48 +04:00
|
|
|
IRQ_OFF;
|
2011-04-16 02:33:19 +04:00
|
|
|
/* Load the loadable segments from the binary */
|
|
|
|
for (uintptr_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
|
|
|
|
/* read a section header */
|
|
|
|
Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)header + (header->e_shoff + x));
|
|
|
|
if (shdr->sh_addr) {
|
|
|
|
/* If this is a loadable section, load it up. */
|
2011-12-09 01:25:48 +04:00
|
|
|
if (shdr->sh_addr < current_process->image.entry) {
|
2011-04-16 02:33:19 +04:00
|
|
|
/* If this is the lowest entry point, store it for memory reasons */
|
2011-12-09 01:25:48 +04:00
|
|
|
current_process->image.entry = shdr->sh_addr;
|
2011-04-16 02:33:19 +04:00
|
|
|
}
|
2011-12-09 01:25:48 +04:00
|
|
|
if (shdr->sh_addr + shdr->sh_size - current_process->image.entry > current_process->image.size) {
|
2011-04-16 02:33:19 +04:00
|
|
|
/* We also store the total size of the memory region used by the application */
|
2011-12-09 01:25:48 +04:00
|
|
|
current_process->image.size = shdr->sh_addr + shdr->sh_size - current_process->image.entry;
|
2011-04-09 00:27:12 +04:00
|
|
|
}
|
2011-12-09 01:25:48 +04:00
|
|
|
for (uintptr_t i = 0; i < shdr->sh_size + 0x2000; i += 0x1000) {
|
2011-04-16 02:33:19 +04:00
|
|
|
/* This doesn't care if we already allocated this page */
|
|
|
|
alloc_frame(get_page(shdr->sh_addr + i, 1, current_directory), 0, 1);
|
|
|
|
}
|
2011-05-01 04:09:29 +04:00
|
|
|
if (shdr->sh_type == SHT_NOBITS) {
|
2011-05-03 06:47:05 +04:00
|
|
|
/* This is the .bss, zero it */
|
2011-05-01 04:09:29 +04:00
|
|
|
memset((void *)(shdr->sh_addr), 0x0, shdr->sh_size);
|
|
|
|
} else {
|
|
|
|
/* Copy the section into memory */
|
|
|
|
memcpy((void *)(shdr->sh_addr), (void *)((uintptr_t)header + shdr->sh_offset), shdr->sh_size);
|
|
|
|
}
|
2011-04-09 00:27:12 +04:00
|
|
|
}
|
2011-04-16 02:33:19 +04:00
|
|
|
}
|
2011-12-09 01:25:48 +04:00
|
|
|
IRQ_ON;
|
2011-04-09 00:27:12 +04:00
|
|
|
|
2011-04-16 02:33:19 +04:00
|
|
|
/* Store the entry point to the code segment */
|
|
|
|
uintptr_t entry = (uintptr_t)header->e_entry;
|
2011-04-15 05:32:19 +04:00
|
|
|
|
2011-04-16 02:33:19 +04:00
|
|
|
/* Free the space we used for the ELF headers and files */
|
|
|
|
free(header);
|
|
|
|
close_fs(file);
|
2011-04-09 02:53:52 +04:00
|
|
|
|
2011-04-18 02:44:29 +04:00
|
|
|
for (uintptr_t stack_pointer = 0x10000000; stack_pointer < 0x100F0000; stack_pointer += 0x1000) {
|
|
|
|
alloc_frame(get_page(stack_pointer, 1, current_directory), 0, 1);
|
|
|
|
}
|
|
|
|
|
2011-12-09 01:25:48 +04:00
|
|
|
uintptr_t heap = current_process->image.entry + current_process->image.size;
|
2011-04-18 02:44:29 +04:00
|
|
|
alloc_frame(get_page(heap, 1, current_directory), 0, 1);
|
|
|
|
char ** argv_ = (char **)heap;
|
|
|
|
heap += sizeof(char *) * argc;
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
alloc_frame(get_page(heap, 1, current_directory), 0, 1);
|
|
|
|
argv_[i] = (char *)heap;
|
|
|
|
memcpy((void *)heap, argv[i], strlen(argv[i]) * sizeof(char) + 1);
|
|
|
|
heap += strlen(argv[i]) + 1;
|
|
|
|
}
|
|
|
|
|
2011-12-09 01:25:48 +04:00
|
|
|
current_process->image.heap = heap; /* heap end */
|
|
|
|
current_process->image.heap_actual = heap + (0x1000 - heap % 0x1000);
|
2011-12-14 02:06:45 +04:00
|
|
|
current_process->image.user_stack = 0x100EFFFF;
|
2011-12-09 01:25:48 +04:00
|
|
|
while (current_process->fds.length < 3) {
|
|
|
|
process_append_fd((process_t *)current_process, NULL);
|
|
|
|
}
|
2011-04-18 03:28:40 +04:00
|
|
|
|
2011-04-16 02:33:19 +04:00
|
|
|
/* Go go go */
|
2011-04-18 02:44:29 +04:00
|
|
|
enter_user_jmp(entry, argc, argv_, 0x100EFFFF);
|
2011-04-09 00:27:12 +04:00
|
|
|
|
2011-04-15 05:32:19 +04:00
|
|
|
/* We should never reach this code */
|
2011-04-16 02:33:19 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
system(
|
|
|
|
char * path, /* Path to the executable to run */
|
|
|
|
int argc, /* Argument count (ie, /bin/echo hello world = 3) */
|
|
|
|
char ** argv /* Argument strings (including executable path) */
|
|
|
|
) {
|
|
|
|
int child = fork();
|
|
|
|
if (child == 0) {
|
|
|
|
exec(path,argc,argv);
|
2011-12-07 06:36:25 +04:00
|
|
|
kexit(-1);
|
2011-04-16 02:33:19 +04:00
|
|
|
return -1;
|
2011-04-09 00:27:12 +04:00
|
|
|
} else {
|
2011-04-16 02:33:19 +04:00
|
|
|
/* We are system(), so we need to wait for the child
|
2011-04-15 05:32:19 +04:00
|
|
|
* application to exit before we can continue. */
|
|
|
|
/* Get the child task. */
|
2011-12-09 01:25:48 +04:00
|
|
|
process_t * volatile child_task = process_from_pid(child);
|
2011-04-15 05:32:19 +04:00
|
|
|
/* If the child task doesn't exist, bail */
|
2011-04-09 02:53:52 +04:00
|
|
|
if (!child_task) return -1;
|
2011-04-15 05:32:19 +04:00
|
|
|
/* Wait until it finishes (this is stupidly memory intensive,
|
|
|
|
* but we haven't actually implemented wait() yet, so there's
|
|
|
|
* not all that much we can do right now. */
|
2011-04-09 00:27:12 +04:00
|
|
|
while (child_task->finished == 0) {
|
|
|
|
if (child_task->finished != 0) break;
|
|
|
|
}
|
2011-04-15 05:32:19 +04:00
|
|
|
/* Grab the child's return value */
|
2011-12-16 03:21:28 +04:00
|
|
|
int ret = child_task->status;
|
|
|
|
free(child_task);
|
|
|
|
return ret;
|
2011-04-09 00:27:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|