2015-04-06 19:40:12 +08:00
|
|
|
#include <stdarg.h>
|
2015-08-02 11:41:05 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2015-04-06 19:40:12 +08:00
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
#include <link.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <dyld.h>
|
|
|
|
#include <unwind.h>
|
|
|
|
|
|
|
|
#include "ksupport.h"
|
|
|
|
#include "kloader.h"
|
2015-04-05 22:04:50 +08:00
|
|
|
#include "mailbox.h"
|
|
|
|
#include "messages.h"
|
2015-08-02 11:41:05 +08:00
|
|
|
#include "bridge.h"
|
|
|
|
#include "artiq_personality.h"
|
|
|
|
#include "ttl.h"
|
2015-04-04 22:08:32 +08:00
|
|
|
#include "dds.h"
|
2015-08-02 11:41:05 +08:00
|
|
|
#include "rtio.h"
|
2015-04-04 22:08:32 +08:00
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
/* compiler-rt symbols */
|
|
|
|
extern void __divsi3, __modsi3, __ledf2, __gedf2, __unorddf2, __eqdf2, __ltdf2,
|
|
|
|
__nedf2, __gtdf2, __negsf2, __negdf2, __addsf3, __subsf3, __mulsf3,
|
|
|
|
__divsf3, __lshrdi3, __muldi3, __divdi3, __ashldi3, __ashrdi3,
|
|
|
|
__udivmoddi4, __floatsisf, __floatunsisf, __fixsfsi, __fixunssfsi,
|
|
|
|
__adddf3, __subdf3, __muldf3, __divdf3, __floatsidf, __floatunsidf,
|
|
|
|
__floatdidf, __fixdfsi, __fixdfdi, __fixunsdfsi, __clzsi2, __ctzsi2,
|
|
|
|
__udivdi3, __umoddi3, __moddi3;
|
|
|
|
|
|
|
|
/* artiq_personality symbols */
|
|
|
|
extern void __artiq_personality;
|
|
|
|
|
|
|
|
struct symbol {
|
|
|
|
const char *name;
|
|
|
|
void *addr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct symbol runtime_exports[] = {
|
|
|
|
/* compiler-rt */
|
|
|
|
{"divsi3", &__divsi3},
|
|
|
|
{"modsi3", &__modsi3},
|
|
|
|
{"ledf2", &__ledf2},
|
|
|
|
{"gedf2", &__gedf2},
|
|
|
|
{"unorddf2", &__unorddf2},
|
|
|
|
{"eqdf2", &__eqdf2},
|
|
|
|
{"ltdf2", &__ltdf2},
|
|
|
|
{"nedf2", &__nedf2},
|
|
|
|
{"gtdf2", &__gtdf2},
|
|
|
|
{"negsf2", &__negsf2},
|
|
|
|
{"negdf2", &__negdf2},
|
|
|
|
{"addsf3", &__addsf3},
|
|
|
|
{"subsf3", &__subsf3},
|
|
|
|
{"mulsf3", &__mulsf3},
|
|
|
|
{"divsf3", &__divsf3},
|
|
|
|
{"lshrdi3", &__lshrdi3},
|
|
|
|
{"muldi3", &__muldi3},
|
|
|
|
{"divdi3", &__divdi3},
|
|
|
|
{"ashldi3", &__ashldi3},
|
|
|
|
{"ashrdi3", &__ashrdi3},
|
|
|
|
{"udivmoddi4", &__udivmoddi4},
|
|
|
|
{"floatsisf", &__floatsisf},
|
|
|
|
{"floatunsisf", &__floatunsisf},
|
|
|
|
{"fixsfsi", &__fixsfsi},
|
|
|
|
{"fixunssfsi", &__fixunssfsi},
|
|
|
|
{"adddf3", &__adddf3},
|
|
|
|
{"subdf3", &__subdf3},
|
|
|
|
{"muldf3", &__muldf3},
|
|
|
|
{"divdf3", &__divdf3},
|
|
|
|
{"floatsidf", &__floatsidf},
|
|
|
|
{"floatunsidf", &__floatunsidf},
|
|
|
|
{"floatdidf", &__floatdidf},
|
|
|
|
{"fixdfsi", &__fixdfsi},
|
|
|
|
{"fixdfdi", &__fixdfdi},
|
|
|
|
{"fixunsdfsi", &__fixunsdfsi},
|
|
|
|
{"clzsi2", &__clzsi2},
|
|
|
|
{"ctzsi2", &__ctzsi2},
|
|
|
|
{"udivdi3", &__udivdi3},
|
|
|
|
{"umoddi3", &__umoddi3},
|
|
|
|
{"moddi3", &__moddi3},
|
|
|
|
|
|
|
|
/* exceptions */
|
|
|
|
{"_Unwind_Resume", &_Unwind_Resume},
|
|
|
|
{"__artiq_personality", &__artiq_personality},
|
|
|
|
{"__artiq_raise", &__artiq_raise},
|
|
|
|
{"__artiq_reraise", &__artiq_reraise},
|
|
|
|
|
|
|
|
/* proxified syscalls */
|
|
|
|
{"now_init", &now_init},
|
|
|
|
{"now_save", &now_save},
|
|
|
|
|
|
|
|
{"watchdog_set", &watchdog_set},
|
|
|
|
{"watchdog_clear", &watchdog_clear},
|
|
|
|
|
|
|
|
{"log", &log},
|
|
|
|
{"lognonl", &lognonl},
|
2015-08-09 07:25:58 +08:00
|
|
|
{"send_rpc", &send_rpc},
|
2015-08-02 11:41:05 +08:00
|
|
|
|
|
|
|
/* direct syscalls */
|
|
|
|
{"rtio_get_counter", &rtio_get_counter},
|
|
|
|
|
|
|
|
{"ttl_set_o", &ttl_set_o},
|
|
|
|
{"ttl_set_oe", &ttl_set_oe},
|
|
|
|
{"ttl_set_sensitivity", &ttl_set_sensitivity},
|
|
|
|
{"ttl_get", &ttl_get},
|
|
|
|
{"ttl_clock_set", &ttl_clock_set},
|
|
|
|
|
|
|
|
{"dds_init", &dds_init},
|
|
|
|
{"dds_batch_enter", &dds_batch_enter},
|
|
|
|
{"dds_batch_exit", &dds_batch_exit},
|
|
|
|
{"dds_set", &dds_set},
|
|
|
|
|
|
|
|
/* end */
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* called by libunwind */
|
|
|
|
int fprintf(FILE *stream, const char *fmt, ...)
|
2015-04-03 17:44:56 +08:00
|
|
|
{
|
2015-08-02 11:41:05 +08:00
|
|
|
struct msg_log request;
|
2015-04-05 22:04:50 +08:00
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
request.type = MESSAGE_TYPE_LOG;
|
|
|
|
request.fmt = fmt;
|
|
|
|
request.no_newline = 1;
|
|
|
|
va_start(request.args, fmt);
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
va_end(request.args);
|
|
|
|
|
|
|
|
return 0;
|
2015-04-03 17:44:56 +08:00
|
|
|
}
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
/* called by libunwind */
|
|
|
|
int dladdr (const void *address, Dl_info *info) {
|
|
|
|
/* we don't try to resolve names */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called by libunwind */
|
|
|
|
int dl_iterate_phdr (int (*callback) (struct dl_phdr_info *, size_t, void *), void *data) {
|
|
|
|
Elf32_Ehdr *ehdr;
|
|
|
|
struct dl_phdr_info phdr_info;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
ehdr = (Elf32_Ehdr *)(KERNELCPU_EXEC_ADDRESS - KSUPPORT_HEADER_SIZE);
|
|
|
|
phdr_info = (struct dl_phdr_info){
|
|
|
|
.dlpi_addr = 0, /* absolutely linked */
|
|
|
|
.dlpi_name = "<ksupport>",
|
|
|
|
.dlpi_phdr = (Elf32_Phdr*) ((intptr_t)ehdr + ehdr->e_phoff),
|
|
|
|
.dlpi_phnum = ehdr->e_phnum,
|
|
|
|
};
|
|
|
|
retval = callback(&phdr_info, sizeof(phdr_info), data);
|
|
|
|
if(retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
ehdr = (Elf32_Ehdr *)KERNELCPU_PAYLOAD_ADDRESS;
|
|
|
|
phdr_info = (struct dl_phdr_info){
|
|
|
|
.dlpi_addr = KERNELCPU_PAYLOAD_ADDRESS,
|
|
|
|
.dlpi_name = "<kernel>",
|
|
|
|
.dlpi_phdr = (Elf32_Phdr*) ((intptr_t)ehdr + ehdr->e_phoff),
|
|
|
|
.dlpi_phnum = ehdr->e_phnum,
|
|
|
|
};
|
|
|
|
retval = callback(&phdr_info, sizeof(phdr_info), data);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Elf32_Addr resolve_runtime_export(const char *name) {
|
|
|
|
const struct symbol *sym = runtime_exports;
|
|
|
|
while(sym->name) {
|
|
|
|
if(!strcmp(sym->name, name))
|
|
|
|
return (Elf32_Addr)sym->addr;
|
|
|
|
++sym;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exception_handler(unsigned long vect, unsigned long *regs,
|
|
|
|
unsigned long pc, unsigned long ea);
|
|
|
|
void exception_handler(unsigned long vect, unsigned long *regs,
|
|
|
|
unsigned long pc, unsigned long ea)
|
|
|
|
{
|
|
|
|
artiq_raise_from_c("InternalError",
|
|
|
|
"Hardware exception {0} at PC {1}, EA {2}",
|
|
|
|
vect, pc, ea);
|
|
|
|
}
|
2015-04-03 17:44:56 +08:00
|
|
|
|
|
|
|
int main(void);
|
|
|
|
int main(void)
|
|
|
|
{
|
2015-08-07 13:49:57 +08:00
|
|
|
struct msg_load_request *request = mailbox_receive();
|
|
|
|
struct msg_load_reply load_reply = {
|
|
|
|
.type = MESSAGE_TYPE_LOAD_REPLY,
|
|
|
|
.error = NULL
|
|
|
|
};
|
2015-04-16 21:47:05 +08:00
|
|
|
|
2015-08-07 13:49:57 +08:00
|
|
|
if(request == NULL) {
|
2015-04-16 21:47:05 +08:00
|
|
|
bridge_main();
|
2015-08-02 11:41:05 +08:00
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:49:57 +08:00
|
|
|
if(request->library != NULL) {
|
|
|
|
if(!dyld_load(request->library, KERNELCPU_PAYLOAD_ADDRESS,
|
|
|
|
resolve_runtime_export, request->library_info,
|
|
|
|
&load_reply.error)) {
|
|
|
|
mailbox_send(&load_reply);
|
2015-08-02 11:41:05 +08:00
|
|
|
while(1);
|
2015-04-16 21:47:05 +08:00
|
|
|
}
|
2015-04-04 22:08:32 +08:00
|
|
|
}
|
2015-08-02 11:41:05 +08:00
|
|
|
|
2015-08-07 13:49:57 +08:00
|
|
|
if(request->run_kernel) {
|
|
|
|
void (*kernel_init)() = request->library_info->init;
|
2015-08-02 11:41:05 +08:00
|
|
|
|
2015-08-07 13:49:57 +08:00
|
|
|
mailbox_send_and_wait(&load_reply);
|
|
|
|
kernel_init();
|
2015-08-02 11:41:05 +08:00
|
|
|
|
2015-08-07 13:49:57 +08:00
|
|
|
struct msg_base finished_reply;
|
|
|
|
finished_reply.type = MESSAGE_TYPE_FINISHED;
|
|
|
|
mailbox_send_and_wait(&finished_reply);
|
|
|
|
} else {
|
|
|
|
mailbox_send(&load_reply);
|
2015-08-02 11:41:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called from __artiq_personality */
|
|
|
|
void __artiq_terminate(struct artiq_exception *artiq_exn,
|
|
|
|
struct artiq_backtrace_item *backtrace,
|
|
|
|
size_t backtrace_size) {
|
|
|
|
struct msg_exception msg;
|
|
|
|
|
|
|
|
msg.type = MESSAGE_TYPE_EXCEPTION;
|
|
|
|
msg.exception = artiq_exn;
|
|
|
|
msg.backtrace = backtrace;
|
|
|
|
msg.backtrace_size = backtrace_size;
|
|
|
|
mailbox_send(&msg);
|
|
|
|
|
2015-04-04 22:08:32 +08:00
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
2015-05-02 23:41:49 +08:00
|
|
|
long long int now_init(void)
|
|
|
|
{
|
|
|
|
struct msg_base request;
|
|
|
|
struct msg_now_init_reply *reply;
|
|
|
|
long long int now;
|
|
|
|
|
|
|
|
request.type = MESSAGE_TYPE_NOW_INIT_REQUEST;
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
|
|
|
|
reply = mailbox_wait_and_receive();
|
2015-08-02 11:41:05 +08:00
|
|
|
if(reply->type != MESSAGE_TYPE_NOW_INIT_REPLY) {
|
2015-08-08 21:01:08 +08:00
|
|
|
log("Malformed MESSAGE_TYPE_NOW_INIT_REQUEST reply type %d",
|
|
|
|
reply->type);
|
2015-08-02 11:41:05 +08:00
|
|
|
while(1);
|
|
|
|
}
|
2015-05-02 23:41:49 +08:00
|
|
|
now = reply->now;
|
|
|
|
mailbox_acknowledge();
|
|
|
|
|
|
|
|
if(now < 0) {
|
|
|
|
rtio_init();
|
2015-07-27 10:50:25 +08:00
|
|
|
now = rtio_get_counter() + (125000 << RTIO_FINE_TS_WIDTH);
|
2015-05-02 23:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
void now_save(long long int now)
|
|
|
|
{
|
|
|
|
struct msg_now_save request;
|
|
|
|
|
|
|
|
request.type = MESSAGE_TYPE_NOW_SAVE;
|
|
|
|
request.now = now;
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:58:37 +08:00
|
|
|
int watchdog_set(int ms)
|
|
|
|
{
|
|
|
|
struct msg_watchdog_set_request request;
|
|
|
|
struct msg_watchdog_set_reply *reply;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
request.type = MESSAGE_TYPE_WATCHDOG_SET_REQUEST;
|
|
|
|
request.ms = ms;
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
|
|
|
|
reply = mailbox_wait_and_receive();
|
2015-08-02 11:41:05 +08:00
|
|
|
if(reply->type != MESSAGE_TYPE_WATCHDOG_SET_REPLY) {
|
2015-08-08 21:01:08 +08:00
|
|
|
log("Malformed MESSAGE_TYPE_WATCHDOG_SET_REQUEST reply type %d",
|
|
|
|
reply->type);
|
2015-08-02 11:41:05 +08:00
|
|
|
while(1);
|
|
|
|
}
|
2015-04-29 12:58:37 +08:00
|
|
|
id = reply->id;
|
|
|
|
mailbox_acknowledge();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void watchdog_clear(int id)
|
|
|
|
{
|
|
|
|
struct msg_watchdog_clear request;
|
|
|
|
|
|
|
|
request.type = MESSAGE_TYPE_WATCHDOG_CLEAR;
|
|
|
|
request.id = id;
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
}
|
|
|
|
|
2015-08-09 07:25:58 +08:00
|
|
|
int send_rpc(int service, const char *tag, ...)
|
2015-04-04 22:08:32 +08:00
|
|
|
{
|
2015-08-09 07:25:58 +08:00
|
|
|
struct msg_rpc_send request;
|
2015-04-06 19:40:12 +08:00
|
|
|
|
2015-08-09 07:25:58 +08:00
|
|
|
request.type = MESSAGE_TYPE_RPC_SEND;
|
2015-08-09 07:17:19 +08:00
|
|
|
request.service = service;
|
|
|
|
request.tag = tag;
|
|
|
|
va_start(request.args, tag);
|
2015-04-06 19:40:12 +08:00
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
va_end(request.args);
|
|
|
|
|
2015-08-09 07:25:58 +08:00
|
|
|
// struct msg_base *reply;
|
|
|
|
// reply = mailbox_wait_and_receive();
|
2015-08-09 07:17:19 +08:00
|
|
|
// if(reply->type == MESSAGE_TYPE_RPC_REPLY) {
|
|
|
|
// int result = ((struct msg_rpc_reply *)reply)->result;
|
|
|
|
// mailbox_acknowledge();
|
|
|
|
// return result;
|
|
|
|
// } else if(reply->type == MESSAGE_TYPE_RPC_EXCEPTION) {
|
|
|
|
// struct artiq_exception exception;
|
|
|
|
// memcpy(&exception, ((struct msg_rpc_exception *)reply)->exception,
|
|
|
|
// sizeof(struct artiq_exception));
|
|
|
|
// mailbox_acknowledge();
|
|
|
|
// __artiq_raise(&exception);
|
|
|
|
// } else {
|
2015-08-09 07:25:58 +08:00
|
|
|
// log("Malformed MESSAGE_TYPE_RPC_REQUEST reply type %d",
|
|
|
|
// reply->type);
|
2015-08-08 21:01:08 +08:00
|
|
|
while(1);
|
2015-08-09 07:17:19 +08:00
|
|
|
// }
|
2015-04-06 19:40:12 +08:00
|
|
|
}
|
|
|
|
|
2015-08-02 11:33:12 +08:00
|
|
|
void lognonl(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct msg_log request;
|
|
|
|
|
|
|
|
request.type = MESSAGE_TYPE_LOG;
|
|
|
|
request.fmt = fmt;
|
|
|
|
request.no_newline = 1;
|
|
|
|
va_start(request.args, fmt);
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
va_end(request.args);
|
|
|
|
}
|
|
|
|
|
2015-04-22 01:31:31 +08:00
|
|
|
void log(const char *fmt, ...)
|
2015-04-06 19:40:12 +08:00
|
|
|
{
|
|
|
|
struct msg_log request;
|
|
|
|
|
|
|
|
request.type = MESSAGE_TYPE_LOG;
|
|
|
|
request.fmt = fmt;
|
2015-08-02 11:33:12 +08:00
|
|
|
request.no_newline = 0;
|
2015-04-06 19:40:12 +08:00
|
|
|
va_start(request.args, fmt);
|
|
|
|
mailbox_send_and_wait(&request);
|
|
|
|
va_end(request.args);
|
2015-04-03 17:44:56 +08:00
|
|
|
}
|