46,9 → 46,11 |
#include <stdio.h> |
#include <stdlib.h> |
#include <unistd.h> |
#include <bool.h> |
#include <fcntl.h> |
#include <sys/types.h> |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/loader.h> |
#include <loader/pcb.h> |
#include <errno.h> |
58,11 → 60,7 |
#include <elf.h> |
#include <elf_load.h> |
|
/** |
* Bias used for loading the dynamic linker. This will be soon replaced |
* by automatic placement. |
*/ |
#define RTLD_BIAS 0x80000 |
#define DPRINTF(...) |
|
/** Pathname of the file that will be loaded */ |
static char *pathname = NULL; |
77,6 → 75,36 |
/** Buffer holding all arguments */ |
static char *arg_buf = NULL; |
|
static elf_info_t prog_info; |
static elf_info_t interp_info; |
|
static bool is_dyn_linked; |
|
/** Used to limit number of connections to one. */ |
static bool connected; |
|
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
task_id_t task_id; |
size_t len; |
|
task_id = task_get_id(); |
|
if (!ipc_data_read_receive(&callid, &len)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
if (len > sizeof(task_id)) |
len = sizeof(task_id); |
|
ipc_data_read_finalize(callid, &task_id, len); |
ipc_answer_0(rid, EOK); |
} |
|
|
/** Receive a call setting pathname of the program to execute. |
* |
* @param rid |
191,30 → 219,23 |
argv[n] = NULL; |
} |
|
|
/** Load and run the previously selected program. |
/** Load the previously selected program. |
* |
* @param rid |
* @param request |
* @return 0 on success, !0 on error. |
*/ |
static int loader_run(ipc_callid_t rid, ipc_call_t *request) |
static int loader_load(ipc_callid_t rid, ipc_call_t *request) |
{ |
int rc; |
|
elf_info_t prog_info; |
elf_info_t interp_info; |
|
// printf("Load program '%s'\n", pathname); |
|
rc = elf_load_file(pathname, 0, &prog_info); |
if (rc < 0) { |
printf("failed to load program\n"); |
DPRINTF("Failed to load executable '%s'.\n", pathname); |
ipc_answer_0(rid, EINVAL); |
return 1; |
} |
|
// printf("Create PCB\n"); |
elf_create_pcb(&prog_info, &pcb); |
|
pcb.argc = argc; |
222,37 → 243,57 |
|
if (prog_info.interp == NULL) { |
/* Statically linked program */ |
// printf("Run statically linked program\n"); |
// printf("entry point: 0x%llx\n", prog_info.entry); |
is_dyn_linked = false; |
ipc_answer_0(rid, EOK); |
close_console(); |
elf_run(&prog_info, &pcb); |
return 0; |
} |
|
printf("Load dynamic linker '%s'\n", prog_info.interp); |
rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info); |
rc = elf_load_file(prog_info.interp, 0, &interp_info); |
if (rc < 0) { |
printf("failed to load dynamic linker\n"); |
DPRINTF("Failed to load interpreter '%s.'\n", |
prog_info.interp); |
ipc_answer_0(rid, EINVAL); |
return 1; |
} |
|
/* |
* Provide dynamic linker with some useful data |
is_dyn_linked = true; |
ipc_answer_0(rid, EOK); |
|
return 0; |
} |
|
|
/** Run the previously loaded program. |
* |
* @param rid |
* @param request |
* @return 0 on success, !0 on error. |
*/ |
pcb.rtld_dynamic = interp_info.dynamic; |
pcb.rtld_bias = RTLD_BIAS; |
static void loader_run(ipc_callid_t rid, ipc_call_t *request) |
{ |
const char *cp; |
|
printf("run dynamic linker\n"); |
printf("entry point: 0x%llx\n", interp_info.entry); |
/* Set the task name. */ |
cp = strrchr(pathname, '/'); |
cp = (cp == NULL) ? pathname : (cp + 1); |
task_set_name(cp); |
|
if (is_dyn_linked == true) { |
/* Dynamically linked program */ |
DPRINTF("Run ELF interpreter.\n"); |
DPRINTF("Entry point: 0x%lx\n", interp_info.entry); |
close_console(); |
|
ipc_answer_0(rid, EOK); |
elf_run(&interp_info, &pcb); |
} else { |
/* Statically linked program */ |
close_console(); |
ipc_answer_0(rid, EOK); |
elf_run(&prog_info, &pcb); |
} |
|
/* Not reached */ |
return 0; |
} |
|
/** Handle loader connection. |
266,23 → 307,42 |
ipc_call_t call; |
int retval; |
|
/* Already have a connection? */ |
if (connected) { |
ipc_answer_0(iid, ELIMIT); |
return; |
} |
|
connected = true; |
|
/* Accept the connection */ |
ipc_answer_0(iid, EOK); |
|
/* Ignore parameters, the connection is already open */ |
(void)iid; (void)icall; |
(void) iid; |
(void) icall; |
|
while (1) { |
callid = async_get_call(&call); |
// printf("received call from phone %d, method=%d\n", |
// call.in_phone_hash, IPC_GET_METHOD(call)); |
|
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
exit(0); |
case LOADER_GET_TASKID: |
loader_get_taskid(callid, &call); |
continue; |
case LOADER_SET_PATHNAME: |
loader_set_pathname(callid, &call); |
continue; |
case LOADER_SET_ARGS: |
loader_set_args(callid, &call); |
continue; |
case LOADER_LOAD: |
loader_load(callid, &call); |
continue; |
case LOADER_RUN: |
loader_run(callid, &call); |
exit(0); |
continue; |
/* Not reached */ |
default: |
retval = ENOENT; |
break; |
289,7 → 349,7 |
} |
if ((callid & IPC_CALLID_NOTIFICATION) == 0 && |
IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) { |
printf("responding EINVAL to method %d\n", |
DPRINTF("Responding EINVAL to method %d.\n", |
IPC_GET_METHOD(call)); |
ipc_answer_0(callid, EINVAL); |
} |
300,31 → 360,20 |
*/ |
int main(int argc, char *argv[]) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t phone_hash; |
ipcarg_t phonead; |
|
/* The first call only communicates the incoming phone hash */ |
callid = ipc_wait_for_call(&call); |
connected = false; |
|
if (IPC_GET_METHOD(call) != LOADER_HELLO) { |
if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) |
ipc_answer_0(callid, EINVAL); |
return 1; |
} |
/* Set a handler of incomming connections. */ |
async_set_client_connection(loader_connection); |
|
ipc_answer_0(callid, EOK); |
phone_hash = call.in_phone_hash; |
/* Register at naming service. */ |
if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0) |
return -1; |
|
/* |
* Up until now async must not be used as it couldn't |
* handle incoming requests. (Which means e.g. printf() |
* cannot be used) |
*/ |
async_new_connection(phone_hash, 0, NULL, loader_connection); |
async_manager(); |
|
/* not reached */ |
/* Never reached */ |
return 0; |
} |
|